<?php
class UsersController extends AppController {

	var $name = 'Users';
	var $components = array('Email', 'Security');

	function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow(array('logout', 'activate', 'request_auth'));
	}

	/* -- FRONT-END -- */
	/**
	 *  The AuthComponent provides the needed functionality
	 *  for login, so you can leave this function blank.
	 */
	function login() {
		// Se è già connesso reindirizza nella home
		if ($this->Session->check('Auth.User'))
		{
			$this->redirect($this->Auth->redirect());
		}
	}

	function logout() {
		$this->redirect($this->Auth->logout());
	}

	function request_auth() {
		if ($this->params['requested'])
		{
			$this->User->recursive = -1;
			return $this->User->read(null, $this->Auth->user('id'));
		}
		$this->redirect('/');
	}

	/* -- BACK-END -- */

	function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function add() {
		// Se è già connesso reindirizza nella home
		if ($this->Session->check('Auth.User'))
		{
			$this->redirect($this->Auth->redirect());
		}

		if (!empty($this->data))
		{
			$this->User->create();
			if ($this->User->save($this->data))
			{
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			}
			else
			{
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		$groups = $this->User->Group->find('list');
		$contexts = $this->User->Context->find('list');
		$areas = $this->User->Area->find('list');
		$charts = $this->User->Chart->find('list');
		$questions = $this->User->Question->find('list');
		$this->set(compact('groups', 'contexts', 'areas', 'charts', 'questions'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
		$groups = $this->User->Group->find('list');
		$contexts = $this->User->Context->find('list');
		$areas = $this->User->Area->find('list');
		$charts = $this->User->Chart->find('list');
		$questions = $this->User->Question->find('list');
		$this->set(compact('groups', 'contexts', 'areas', 'charts', 'questions'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	function admin_index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		$groups = $this->User->Group->find('list');
		$contexts = $this->User->Context->find('list');
		$areas = $this->User->Area->find('list');
		$charts = $this->User->Chart->find('list');
		$questions = $this->User->Question->find('list');
		$this->set(compact('groups', 'contexts', 'areas', 'charts', 'questions'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
		$groups = $this->User->Group->find('list');
		$contexts = $this->User->Context->find('list');
		$areas = $this->User->Area->find('list');
		$charts = $this->User->Chart->find('list');
		$questions = $this->User->Question->find('list');
		$this->set(compact('groups', 'contexts', 'areas', 'charts', 'questions'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>